define("jquery.mobile.tabs", ["jquery.mobile"], function($){
    $.widget("mobile.tabs", $.mobile.widget, {
        options:{
            theme:"c",
            initSelector:":jqmData(role='tabsview')",
            transition:"slide",
            nextButtonText:"Next",
            prevButtonText:"Previous",
            showWizard:true,
        },
        tabs:[],
        buttons:[],
        activeTab:0,
        _create:function(){
            var self = this;
            var tabBar = $("<div>").attr({
                "data-role":"controlgroup", 
                "data-type":"horizontal", 
                "data-mini":'true',
            }).css({
                "text-align":"center"
            });
            
            this.tabs = self.element.children();
            
            $.each(this.tabs, function(k, v){
                var tab = $(v);
                tab.bind("swipeleft", function(){
                    self.next();
                });
                tab.bind("swiperight", function(){
                    self.previous();
                });
                if(self.options.showWizard){
                    self._setupWizard(tab, k);
                }
                var button = self._createButton(tab.children("h3").hide().text());
                button.attr({
                    "data-dest":k,
                }).bind("tap", {
                    self:self
                }, self._tabClicked);
                tabBar.append(button);
                self.buttons[k] = button;
            });
            
            self.element.prepend(tabBar);
            self.show(0);
            self.element.trigger("create");
        },
        show:function(tab){
            tab = tab < this.tabs.length ? tab : this.tabs.length - 1;
            tab = tab > 0 ? tab : 0;
            
            this.buttons[this.activeTab].removeClass("ui-btn-active");
            this.buttons[tab].addClass("ui-btn-active");
            this.tabs.hide();
            this.activeTab = tab;
            $(this.tabs[tab]).show();
        },
        next:function(){
            this.show(this.activeTab + 1);
        },
        previous:function(){
            this.show(this.activeTab - 1);
        },
        _tabClicked:function(e){
            var self = e.data.self;
            var el = $(this);
            var dest = el.attr("data-dest");
            self.show(parseInt(dest));
            return false;
        },
        _createButton:function(text){
            var button = $("<a>").attr({
                "data-role":"button",
                "href":"#",
                "data-theme":this.options.theme
            });
            button.html(text);
            return button;
        },
        _createNextButton:function(){
            var button = this._createButton(this.options.nextButtonText);
            button.attr({
                "data-icon":"arrow-r",
                "data-iconpos":"right"
            });
            button.bind("tap", {
                self:this
            }, function(e){
                e.data.self.next();
            });
            return button;
        },
        _createPrevButton:function(){
            var button = this._createButton(this.options.prevButtonText);
            button.attr({
                "data-icon":"arrow-l",
                "data-iconpos":"left"
            });
            button.bind("tap", {
                self:this
            }, function(e){
                e.data.self.previous();
            });
            return button;
        },
        _setupWizard:function(tab, k){
            var buttonBar = $("<div>").attr({
                "data-role":"controlgroup",
                "data-type":"horizontal"
            }).css({
                "text-align":"center",
                "border-top": "#828282",
                "border-top-color": "rgba(130, 130, 130, .3)",
                "border-top-width": "1px",
                "border-top-style": "solid",
                "margin-top":"10px",
                "padding-top":"10px",
                "border-top-left-radius": 0,
                "border-top-right-radius": 0
            });
            if(k > 0){
                buttonBar.append(this._createPrevButton());
            }
            if(k < this.tabs.length - 1){
                buttonBar.append(this._createNextButton());
            }
            tab.append(buttonBar);
        }
    });
    
    //auto self-init widgets
    $( document ).bind( "pagecreate create", function(e){
        $.mobile.tabs.prototype.enhanceWithin(e.target);
    });
})


